package com.dong.rabbitmq.topic;

import com.dong.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dongjian
 * topic模式 生产者
 *
 */
public class ProducerTopic {


    public static final String EXCHANGE_NAME = "topic_exchange";

    public static void main(String[] args) throws Exception{

        Channel channel = RabbitMqUtil.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        channel.queueDeclare("Q1", false, false, false, null);
        channel.queueDeclare("Q2", false, false, false, null);

        channel.queueBind("Q1", EXCHANGE_NAME, "*.orange.*");
        channel.queueBind("Q2", EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind("Q2", EXCHANGE_NAME, "dong.#");

        Map<String, String> routingKeyMap = new HashMap<>(16);
        routingKeyMap.put("a.orange.b", "队列Q1收到消息");
        routingKeyMap.put("a.b.rabbit", "队列Q2收到消息");
        routingKeyMap.put("a.orange.rabbit","队列Q1Q2收到消息");
        routingKeyMap.put("dong.a.b", "队列Q2收到消息");
        routingKeyMap.forEach((k, v) -> {
            try {
                System.out.println("生产者发送消息：routingKey==>" + k + "消息内容==>" +v);
                channel.basicPublish(EXCHANGE_NAME, k, null, v.getBytes("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
